package net.pingfang.devicelock.Config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ErrorMessage;

@Configuration
@Slf4j
public class ErrorConfig {

    private final TaskExecutor taskExecutor;

    @Autowired  // 当有多个同名Bean时需显式指定
    public ErrorConfig(@Qualifier("tcpTaskExecutor") TaskExecutor tcpTaskExecutor    ) {
        this.taskExecutor = tcpTaskExecutor;
    }

    @Bean
    public MessageChannel errorChannel() {
        return new PublishSubscribeChannel(taskExecutor);
    }

    @ServiceActivator(inputChannel = "errorChannel")
    public void handleError(ErrorMessage errorMessage) {
        Throwable cause = errorMessage.getPayload();
        log.error("TCP通道异常{}",cause.getMessage());
    }
}
